package com.raja.servingStation.common.redis;

import com.raja.servingStation.common.redis.entity.CarNormal;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class RedisUtilData {
	
	private static Logger log = Logger.getLogger(RedisUtilData.class);


	public static CarNormal getCarNormalCache(byte [] buf) {
		CarNormal normal = new CarNormal();
		try {
			if (buf == null) {
//				log.error("[实时]未获取到通用数据");
				return normal;
			}
			int dataVer = MGDecoder.byte1ToLong(buf[0]);
			int onlineTag = MGDecoder.byte1ToLong(buf[1]);
			long loginIp = MGDecoder.byte4ToLong(buf,2);

			if (buf.length > 14) {
				long firstMileage = MGDecoder.byte4ToLong(buf,14);
				long firstMileageTime = MGDecoder.byte4ToLong(buf,18);

				normal.setFirstMileage(Float.parseFloat(firstMileage+""));
				normal.setFirstMileageTime(firstMileageTime);

			}
			if(buf.length > 22){
				int assemNo = MGDecoder.byte1ToLong(buf[22]);
				if(assemNo < 0 || assemNo > 250){
					assemNo = 0;
				}
				normal.setAssemNo(assemNo);
			}
//			log.error(key+"=====>[普通数据]==>"+dataVer+"===>"+onlineTag+"===>"+loginIp+"====>"+firstMileage+"====>"+firstMileageTime);
			normal.setDataVer(dataVer);
			normal.setOnlineTag(onlineTag);
			normal.setLoginIp(loginIp+"");

		} catch (Exception e) {
			log.error("get getCarNormalCache error!");
		}
		return normal;
	}


	public static List<MonitorData> formatData(List<MonitorData> mdList ,List<String> keyList ,List<byte []> valList , int num){
		int nLen = mdList.size();
		List<MonitorData> sdList = new ArrayList<MonitorData>();
		for (int i = 0; i < nLen; i++) {
			int index = i * num;
			MonitorData md = mdList.get(i);
			index++;
			//普通数据
			CarNormal cn = getCarNormalCache(valList.get(index++));
			//定位数据
//			CarGps gps = getCarGpsCache(valList.get(index++));
			if (md.getSt() != null && md.getSt() == 2
					&& cn.getOnlineTag() != null
					&& cn.getOnlineTag() == 2) { // 如果上一次的状态是离线，并且目前的状态还是离线，则在前端不需要更新状态

				System.out.println("如果上一次的状态是离线，并且目前的状态还是离线，则在前端不需要更新状态");
				continue;
			}
			md.setSt(0);
			if (cn != null && cn.getOnlineTag() != null) {
				md.setSt(cn.getOnlineTag());// 车辆状态 0-未知，1-在线，2-离线
			}
			sdList.add(md);
		}

		return sdList;
	}

	public static MonitorData formatData(MonitorData pmd ,List<String> keyList ,List<byte []> valList , int num){
		int nLen = 1;
		int index = 0 * num;
		MonitorData md = pmd;
		index++;
		//普通数据
		CarNormal cn = getCarNormalCache(valList.get(index++));
		//定位数据
//			CarGps gps = getCarGpsCache(valList.get(index++));
		if (md.getSt() != null && md.getSt() == 2
				&& cn.getOnlineTag() != null
				&& cn.getOnlineTag() == 2) { // 如果上一次的状态是离线，并且目前的状态还是离线，则在前端不需要更新状态

			System.out.println("如果上一次的状态是离线，并且目前的状态还是离线，则在前端不需要更新状态");
		}
		md.setSt(0);
		if (cn != null && cn.getOnlineTag() != null) {
			md.setSt(cn.getOnlineTag());// 车辆状态 0-未知，1-在线，2-离线
		}

		return md;
	}


}
